/* 用户相关业务逻辑处理 */
var fs = require('fs')

const { UserModel } = require('./dao/model.js')
const UserService = {
  // 登录业务逻辑处理
  login(req, res, next) {
    // 从请求中获取登录的用户名与密码
    const { userName, password } = req.body // POST请求中传递的数据
    UserModel.findAll({
      where: {
        nickname: userName
      }
    }).then(data => {
      if (data.length === 1) {
        // 数据库中恰好有一个满足条件的用户
        // 比较密码
        if (data[0].password === password) {
          // 将登录成功的用户信息保存到 session 中
          req.session.loginUser = userName
          res.json({
            res_code: 200,
            res_error: '',
            data: {
              messagge: '登录成功',
              status: 1,
              haslogin: true,
              userid: data[0].userid,
              role_id: data[0].role_id,
              username: data[0].nickname,
              email: data[0].email,
              rolename: data[0].rolename,
              avatar: data[0].avatar,
              collect: data[0].collect,
              borrow: data[0].borrow,
              mybooks: data[0].mybooks
            }
          })
        } else {
          // 密码不同
          res.json({
            res_code: 200,
            res_error: '',
            data: {
              status: 0,
              message: '密码错误'
            }
          })
        }
      } else {
        // 用户名错误
        res.json({
          res_code: 200,
          res_error: '',
          data: {
            status: 0,
            message: '用户名错误'
          }
        })
      }
    })
  },
  // 用户注册业务逻辑处理
  register(req, res, next) {
    const { email, password, userName, imageUrl } = req.body
    const base64Data = imageUrl.replace(/^data:image\/\w+;base64,/, '')
    const avatarBuffer = Buffer.from(base64Data, 'base64')
    fs.writeFile(`./src/assets/avatar/${userName}.jpg`, avatarBuffer, function(
      err
    ) {
      if (err) {
        console.log(err)
        console.log('保存失败')
      } else {
        console.log('保存成功')
      }
    })
    //注册
    UserModel.findAll({
      where: {
        email
      }
    }).then(data => {
      if (data.length > 0) {
        res.json({
          res_code: 200,
          res_error: '',
          data: {
            status: 0,
            message: '该邮箱已被注册',
            email: data[0].email
          }
        })
      } else {
        UserModel.findAll({
          where: {
            nickname: userName
          }
        }).then(data => {
          if (data.length > 0) {
            res.json({
              res_code: 200,
              res_error: '',
              data: {
                status: 0,
                message: '该用户名已被注册',
                username: data[0].nickname
              }
            })
          } else {
            // res.json(avatar)
            UserModel.create({
              nickname: userName,
              password,
              userid: email,
              rolename: '普通用户',
              role_id: '1',
              email,
              avatar: `../../assets/avatar/${userName}.jpg`
            }).then(data => {
              // 将注册成功的用户信息保存到 session 中
              req.session.loginUser = email
              res.json({
                res_code: 200,
                res_error: '',
                data: {
                  status: 1,
                  message: '用户注册成功',
                  data
                }
              })
            })
          }
        })
      }
    })
  },

  logout(req, res, next) {
    req.session.loginUser = null
    res.json({
      res_code: 200,
      res_error: '',
      data: {
        status: 1,
        message: '用户注销成功',
        haslogin: false
      }
    })
  }
}

module.exports = UserService
